package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.file.service.impl.MinIOFileStorageService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.threadlocal.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Autowired
    private MinIOFileStorageService minIOFileStorageService;

    /**
     * 素材上传
     *
     * @param file
     * @return
     */
    @Override
    @Transactional
    public ResponseResult upload (MultipartFile file) {

        UUID uuid = UUID.randomUUID ();
        //1.上传图片到minio中
        try {
            String originalFilename = file.getOriginalFilename ();
            if(originalFilename==null){
                return ResponseResult.errorResult (AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR, "未上传图片");
            }
            if (!originalFilename.endsWith (".jpg") && !originalFilename.endsWith (".png")) {
                return ResponseResult.errorResult (AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR, "图片格式错误");
            }
            System.out.println (WmThreadLocalUtil.getUser ().getId ());
            String s = minIOFileStorageService.uploadImgFile ("", uuid + "_" + originalFilename, file.getInputStream ());
            WmMaterial wmMaterial = new WmMaterial ();
            wmMaterial.setUserId (WmThreadLocalUtil.getUser ().getId ());
            wmMaterial.setUrl (s);
            wmMaterial.setType ((short) 0);
            wmMaterial.setIsCollection ((short) 0);
            wmMaterial.setCreatedTime (new java.util.Date ());
            //2.保存到数据库中
            int insert = wmMaterialMapper.insert (wmMaterial);
            if (insert <= 0) {
                return ResponseResult.errorResult (AppHttpCodeEnum.PARAM_INVALID, "文件上次失败");
            }
            wmMaterial = wmMaterialMapper.selectById (wmMaterial.getId ());
            HashMap<String, Object> hashMap = new HashMap<> ();
            hashMap.put ("id", wmMaterial.getId ());
            hashMap.put ("userId", WmThreadLocalUtil.getUser ().getId ());
            hashMap.put ("url", s);
            //根据后缀判断是否为图片
            hashMap.put ("type",0);
            hashMap.put ("isCollection", 0);
            hashMap.put ("collectionTime", wmMaterial.getCreatedTime ());

            //TODO
            return ResponseResult.okResult (hashMap);
        } catch (IOException e) {
            return ResponseResult.errorResult (AppHttpCodeEnum.PARAM_INVALID, "文件上次失败");
        }

    }

    /**
     * 查询素材列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list (WmMaterialDto dto) {
        //TODO
        return null;
    }

    /**
     * 删除素材
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult del (Integer id) {
        //TODO
        return null;
    }

    /**
     * 取消收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult cancel (Integer id) {
        //TODO
        return null;
    }

    /**
     * 收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult collect (Integer id) {
        //TODO
        return null;
    }
}
